package gui;


import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.net.URL;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 * @author Ramy Refky 
 * */
public class MainGui extends JFrame implements MouseListener{
	
	JPanel mainPanel;
	JPanel workAreaPanel;
	WorkArea workArea;
	ActionPanel actionPanel;
	ComponentPanel componentPanel;
	VerticalScrollBar verticalScrollBar;
	HorizontalScrollBar horizontalScrollBar;
	Actions actions;
	JLabel outline;
	
	AboutPanel aboutPanel;
	
	LoadDataHandler ld;
	
	JButton saveButton;
	JButton loadButton;
	JButton newButton;
	
	JButton aboutButton;
	
	URL  basicTabURL= getClass().getResource("graphics/basicTab.png");
	ImageIcon basicTabImageIcon = new ImageIcon(basicTabURL);
	JLabel basicTabLabel;
	
	URL  flipFlopTabURL= getClass().getResource("graphics/flipFlopTab.png");
	ImageIcon flipFlopTabImageIcon = new ImageIcon(flipFlopTabURL);
	JLabel flipFlopTabLabel;
	
	URL  otherTabURL= getClass().getResource("graphics/otherTab.png");
	ImageIcon otherTabImageIcon = new ImageIcon(otherTabURL);
	JLabel otherTabLabel;
	
	
	private static final long serialVersionUID = 1L;
	
	/**
	 * Constructor initializes the main panel and all components.
	 * */
	public MainGui(){
		
		super("Schematic builder");
		setBounds(150,50,1124,624);
		setResizable(false);		
		setVisible(true);
		setLayout(null);
		
		mainPanel = new JPanel();
		mainPanel.setBounds(0,0,1124,624);
		mainPanel.setBackground(Color.lightGray);
		getContentPane().add(mainPanel);
		mainPanel.setVisible(true);
		mainPanel.setLayout(null);
		
		saveButton= new JButton();
		saveButton.setBackground(Color.getHSBColor((float)0.1,(float)0.05,(float)0.9));
		saveButton.setBounds(150,10,90,25);
		JLabel saveLabel =new JLabel("Save As...");
		saveLabel.setBounds(0,0,90,25);
		saveButton.add(saveLabel);
		saveButton.addMouseListener(this);
		mainPanel.add(saveButton);
		
		loadButton= new JButton();
		loadButton.setBackground(Color.getHSBColor((float)0.1,(float)0.05,(float)0.9));
		loadButton.setBounds(80,10,65,25);
		JLabel loadLabel =new JLabel("Load");
		loadLabel.setBounds(0,0,90,25);
		loadButton.add(loadLabel);
		loadButton.addMouseListener(this);
		mainPanel.add(loadButton);
		
		newButton= new JButton();
		newButton.setBackground(Color.getHSBColor((float)0.1,(float)0.05,(float)0.9));
		newButton.setBounds(10,10,65,25);
		JLabel newLabel =new JLabel("New");
		newLabel.setBounds(0,0,90,25);
		newButton.add(newLabel);
		newButton.addMouseListener(this);
		mainPanel.add(newButton);
		
		aboutButton= new JButton();
		aboutButton.setBackground(Color.getHSBColor((float)0.1,(float)0.05,(float)0.9));
		aboutButton.setBounds(1045,0,70,20);
		JLabel aboutLabel = new JLabel("About");
		aboutLabel.setBounds(0,0,90,25);
		aboutButton.add(aboutLabel);
		aboutButton.addMouseListener(this);
		mainPanel.add(aboutButton);
		
		aboutPanel= new AboutPanel();
		mainPanel.add(aboutPanel);
		
		workAreaPanel = new JPanel();
		workAreaPanel.setBounds(79,53,822,493);
		mainPanel.add(workAreaPanel);
		workAreaPanel.setVisible(true);
		workAreaPanel.setLayout(null);
		
		outline = new JLabel();
		outline.setBounds(75,50,830,500);
		outline.setBorder(BorderFactory.createLineBorder(Color.black,3));
		mainPanel.add(outline);
		outline.setVisible(true);
		outline.setLayout(null);
		
		basicTabLabel = new JLabel(basicTabImageIcon);
		basicTabLabel.setBounds(920,35,64,18);
		mainPanel.add(basicTabLabel);
		basicTabLabel.setVisible(true);
		basicTabLabel.addMouseListener(this);
		
		flipFlopTabLabel = new JLabel(flipFlopTabImageIcon);
		flipFlopTabLabel.setBounds(981,35,64,18);
		mainPanel.add(flipFlopTabLabel);
		flipFlopTabLabel.setVisible(true);
		flipFlopTabLabel.addMouseListener(this);
		
		otherTabLabel = new JLabel(otherTabImageIcon);
		otherTabLabel.setBounds(1042,35,64,18);
		mainPanel.add(otherTabLabel);
		otherTabLabel.setVisible(true);
		otherTabLabel.addMouseListener(this);
		
		actions= new Actions();
		
		workArea = new WorkArea(this);
		actionPanel = new ActionPanel(this);
		componentPanel = new ComponentPanel(this);
		verticalScrollBar = new VerticalScrollBar(this);
		horizontalScrollBar = new HorizontalScrollBar(this);
		
		workAreaPanel.add(workArea);
		mainPanel.add(actionPanel);
		mainPanel.add(componentPanel);
		mainPanel.add(verticalScrollBar);
		mainPanel.add(horizontalScrollBar);
		
		mainPanel.setComponentZOrder(basicTabLabel,0);
		mainPanel.setComponentZOrder(componentPanel,1);
		
		validate();
		repaint();
	}
	
	/**
	 * Returns the Actions instance used in MainGui.
	 * */
	public Actions getActions(){
		return actions;
	}
	/**
	 * Returns the ActionPanel instance used in MainGui.
	 * */
	public ActionPanel getActionPanel(){
		return actionPanel;
	}
	/**
	 * Returns the ComponentPanel instance used in MainGui.
	 * */
	public ComponentPanel getComponentPanel(){ 
		return componentPanel;
	}
	/**
	 * Returns the WorkArea instance used in MainGui.
	 * */
	public WorkArea getWorkArea(){
		return workArea;
	}
	/**
	 * Returns the VerticalScrollBar instance used in MainGui.
	 * */
	public VerticalScrollBar getVScrollBar(){
		return verticalScrollBar;
	}
	/**
	 * Returns the HorizontalScrollBar instance used in MainGui.
	 * */
	public HorizontalScrollBar getHScrollBar(){
		return horizontalScrollBar;
	}
	/**
	 * Brings the BasicTab component to the front.
	 * */
	public void focusBasicTab(){
		mainPanel.setComponentZOrder(basicTabLabel,0);
		mainPanel.setComponentZOrder(componentPanel,1);
		
		componentPanel.getBasicPanel().setVisible(true);
		componentPanel.getFFPanel().setVisible(false);
		componentPanel.getOtherPanel().setVisible(false);
		
	}
	/**
	 * Brings the FlipFlopTab component to the front.
	 * */
	public void focusFFTab(){
		mainPanel.setComponentZOrder(flipFlopTabLabel,0);
		mainPanel.setComponentZOrder(componentPanel,1);
		
		componentPanel.getBasicPanel().setVisible(false);
		componentPanel.getFFPanel().setVisible(true);
		componentPanel.getOtherPanel().setVisible(false);
		
	}
	/**
	 * Brings the OtherTab component to the front.
	 * */
	public void focusOtherTab(){
		mainPanel.setComponentZOrder(otherTabLabel,0);
		mainPanel.setComponentZOrder(componentPanel,1);
		
		componentPanel.getBasicPanel().setVisible(false);
		componentPanel.getFFPanel().setVisible(false);
		componentPanel.getOtherPanel().setVisible(true);
		
	}
	/**
	 * Returns the "about" panel.
	 * @return JPanel
	 * */
	public JPanel getAboutPanel(){
		return aboutPanel;
	}
	public static void main(String[]args){
		new MainGui();
	}
	@Override
	public void mouseClicked(MouseEvent e) {}
	@Override
	public void mouseEntered(MouseEvent e) {}
	@Override
	public void mouseExited(MouseEvent e) {}
	@Override
	public void mousePressed(MouseEvent e) {
		aboutPanel.setVisible(false);
		
		if(e.getSource()==basicTabLabel){
			focusBasicTab();
			getComponentPanel().loseFocus();
			getActionPanel().loseFocus();
			getActions().setActionToNothing();
		}
		if(e.getSource()==flipFlopTabLabel){
			focusFFTab();
			getComponentPanel().loseFocus();
			getActionPanel().loseFocus();
			getActions().setActionToNothing();
		}
		if(e.getSource()==otherTabLabel){
			focusOtherTab();
			getComponentPanel().loseFocus();
			getActionPanel().loseFocus();
			getActions().setActionToNothing();
		}
		if(e.getSource()==loadButton){
			LoadDataHandler ld = new LoadDataHandler(this);
			ld.load();
			repaint();
		}
		if(e.getSource()==saveButton){
			SaveDataHandler sd = new SaveDataHandler(this);
			sd.save();
		}
		if(e.getSource()==newButton){
			if(getWorkArea().getAddedComponents().size()>0){
				getWorkArea().getWiringGrid().getWires().clear();
				getWorkArea().getAddedComponents().clear();
				getWorkArea().removeAll();
				getWorkArea().repaint();
			}
		}
		if(e.getSource()==aboutButton){
			aboutPanel.setVisible(true);
		}
		repaint();
	}
	@Override
	public void mouseReleased(MouseEvent e) {}
}
